package com.whg.demo.utils;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.*;
import java.nio.file.Files;
import java.util.UUID;

/**
 * @Author whg
 * @create 2023/9/11 9:17
 * @description：
 */
public class PrintSealUtils {

    public static void main(String[] args) throws DocumentException, IOException {
        File file = new File("D:\\\\home\\\\tonlan\\\\static\\\\printTemplate\\\\template3.pdf");
        InputStream is = new FileInputStream(file);
        String imgPath = "D:\\home\\tonlan\\static\\printTemplate\\fp_seal.png";
        //byte[] bytes = printSeal(toByteArray(is),null,1,170,700,180,90,imgPath);
        byte[] bytes = printSeal(null,file,1,170,700,180,90,imgPath);
        System.out.println(bytes);
    }

    /**
     * 给pdf文件盖章
     * @param bytes pdf原文件流
     * @param file pdf原文件
     * @param page 签章所在页码
     * @param x 签章绝对位置横轴坐标
     * @param y 签章绝对位置纵轴坐标
     * @param width 图片宽度
     * @param height 图片高度
     * @param imgPath 图片路径
     * @return
     */
    public static byte[] printSeal(byte[] bytes,File file,int page,int x,int y,int width,int height,String imgPath) {
        try{
            Boolean delFlag = true;
            if (null != file){
                bytes = getBytesByFile(file);
                delFlag = false;
            }else {
                //创建临时文件，用于生成带签章的pdf文件
                file = Files.createTempFile(UUID.randomUUID().toString(),".pdf").toFile();
            }
            //pdf原文件流
            PdfReader reader = new PdfReader(bytes);
            //临时文件输出流
            OutputStream os = new FileOutputStream(file);
            try{
                //创建pdf编辑器
                PdfStamper stamper = new PdfStamper(reader,os);
                //文件底层编辑对象
                PdfContentByte over = stamper.getUnderContent(page);
                //获取签章图片
                Image img = Image.getInstance(imgPath);
                //图片的绝对位置
                img.setAbsolutePosition(x, y);
                //设置图片长和宽
                img.scaleAbsolute(width, height);
                //设置图片
                over.addImage(img);
                stamper.close();
                bytes = getBytesByFile(file);
            }catch (Exception e){
            }finally {
                if (null != os){
                    os.close();
                }
                if (null != file && delFlag){
                    file.delete();
                }
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        return bytes;
    }

    /**
     * 将流转成字节数组
     * @param input
     * @return
     */
    public static byte[] toByteArray(InputStream input) {
        byte[] bytes = null;
        ByteArrayOutputStream output = new ByteArrayOutputStream();

        try {
            byte[] buffer = new byte[4096];
            boolean var4 = false;

            int n;
            while(-1 != (n = input.read(buffer))) {
                output.write(buffer, 0, n);
            }

            bytes = output.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return bytes;
    }

    //将文件转换成Byte数组
    public static byte[] getBytesByFile(File file) {
        FileInputStream fis = null;
        ByteArrayOutputStream bos = null;
        try {
            fis = new FileInputStream(file);
            bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            byte[] data = bos.toByteArray();
            bos.close();
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try{
                if (null != fis){
                    fis.close();
                }
                if (null != bos){
                    bos.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return null;
    }
}
